
import 'package:baselib/utils/color_base_util.dart';
import 'package:baselib/utils/px_base_util.dart';
import 'package:baselib/utils/string_base_util.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

/**
 * 普通输入文本
 */
class FormInputWidget extends StatefulWidget {
  String title;
  TextStyle titleStyle;
  EdgeInsetsGeometry titleMargin;

  Function onSubmitted;
  Function onChanged;
  List<TextInputFormatter>inputFormatters;
  String defaultCont;
  double width;
  double height;
  bool readOnly;
  TextInputType keyboardType;
  TextInputAction textInputAction;
  EdgeInsetsGeometry contentPadding;
  String hintText;
  Color borderColor;
  double borderRadius;
  BoxDecoration decoration;
  int maxLength;
  int maxLines;
  int minLines;
  bool isNecessary;
  Alignment outAlignment;
  TextAlign inputTextAlign;
  double hintFontSize;
  double fontSize;
  Color hintFontColor;
  Color fontColor;
  FormInputWidget({
    this.title,
    this.outAlignment,this.inputTextAlign,
    this.titleStyle,this.titleMargin,
    this.onSubmitted,this.onChanged,this.inputFormatters,this.defaultCont,this.width,this.height,
    this.readOnly,this.keyboardType,this.textInputAction,this.contentPadding,this.hintText,this.borderColor,
    this.borderRadius,this.decoration,this.maxLength,this.maxLines,this.minLines,this.isNecessary:false,
    this.hintFontSize,this.hintFontColor,this.fontSize,this.fontColor,
  });
  @override
  _FormInputWidgetState createState() => _FormInputWidgetState();
}

class _FormInputWidgetState extends State<FormInputWidget> {
  TextEditingController textEditingController;
  @override
  void initState() {
    // TODO: implement initState
    textEditingController=TextEditingController(text: widget.defaultCont);
    super.initState();
  }

  @override
  void dispose() {
    // TODO: implement dispose
    if(textEditingController!=null)textEditingController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisSize: MainAxisSize.min,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        (widget.title??'').isEmpty?Container():Container(
          child: Row(
            children: [
              (widget.isNecessary??false)?Text(
                '*',
                style: widget.titleStyle??TextStyle(
                    fontSize: PxBaseUtil().px_14,
                    color: ColorBaseUtil().color_red
                ),
              ):Container(),
              Text(
                '${widget.title}',
                style: widget.titleStyle??TextStyle(
                    fontSize: PxBaseUtil().px_14,
                    color: ColorBaseUtil().color_777777
                ),
              ),

            ],
          ),
          margin: widget.titleMargin??EdgeInsets.only(
            bottom: PxBaseUtil().px_10
          ),
        ),
        Container(
          width: widget.width??PxBaseUtil().screenWidth,
          height:widget.height??PxBaseUtil().px_78,
          margin: EdgeInsets.all(PxBaseUtil().px_0),
          decoration: widget.decoration??BoxDecoration(
              borderRadius: BorderRadius.all(Radius.circular(widget.borderRadius??PxBaseUtil().px_4)),
              border: Border.all(
                  color: widget.borderColor??ColorBaseUtil().color_d4d4d4
              ),
          ),
          alignment: widget.outAlignment??Alignment.topLeft,
          child: TextFormField(
            style: TextStyle(
                fontSize: widget.fontSize??PxBaseUtil().px_14,
                color: widget.fontColor??ColorBaseUtil().color_383838
            ),
            enabled:true ,
            textAlign: widget.inputTextAlign??TextAlign.start,
            maxLength: widget.maxLength??100,
            maxLines: widget.maxLines??3,
            minLines: widget.minLines??3,
            obscureText: false,
            autofocus: false,
            controller: textEditingController,
            readOnly: widget.readOnly??false,
            textInputAction: widget.textInputAction??TextInputAction.done,
            keyboardType: widget.keyboardType??TextInputType.text,
            onFieldSubmitted: (val){
              if(widget.onSubmitted!=null)widget.onSubmitted(val);
            },
            onChanged: (value) {
              if(widget.onChanged!=null)widget.onChanged(value);
            },
            cursorColor: ColorBaseUtil().color_blue,
//          // 数字、手机号限制格式为0到9(白名单)， 密码限制不包含汉字（黑名单）
            inputFormatters:widget.inputFormatters,
//                          inputFormatters: (keyboardType == TextInputType.number || keyboardType == TextInputType.phone) ?
//                          [WhitelistingTextInputFormatter(RegExp("[0-9]"))] : [BlacklistingTextInputFormatter(RegExp("[\u4e00-\u9fa5]"))],
            decoration: InputDecoration(
                contentPadding: widget.contentPadding??EdgeInsets.only(
                  left: PxBaseUtil().px_5,
                  right: PxBaseUtil().px_5,
                  top: PxBaseUtil().px_2,
                  bottom: PxBaseUtil().px_2,
                ),
                isDense: true,
                hintText: widget.hintText??'${StringBaseUtil().pleaseinput}',
                hintStyle: TextStyle(
                    fontSize: widget.hintFontSize??PxBaseUtil().px_14,
                    color: widget.hintFontColor??ColorBaseUtil().color_dedede
                ),
                counterText: "",
                focusedBorder: UnderlineInputBorder(
                    borderSide: BorderSide(
                        color: ColorBaseUtil().color_transparent,
                        width: 1
                    )
                ),
                enabledBorder: UnderlineInputBorder(
                    borderSide: BorderSide(
                        color: ColorBaseUtil().color_transparent,
                        width: 1
                    )
                )
            ),
          ),
        )
      ],
    );
  }
}
